<?php
/*
* @Author: hzwlxy
* @Email: 120235331@qq.com
* @Github: http：//www.github.com/siaoynli
* @Date: 2019/6/28 10:15
* @Version:
* @Description:
*/

namespace Modules\Web\Http\Controllers;

use App\Mail\RegisterMail;
use App\Models\Admin;
use App\Models\Permission;
use App\Models\Role;
use Route;
use Enforcer;
use Illuminate\Support\Facades\Auth;


class IndexController extends BaseController
{
    public function index()
    {

        return view("web::welcome");
    }

    public function mail()
    {
        $admin = Admin::find(1);
        \Mail::to($admin)->send(new RegisterMail());
        return view("web::welcome");
    }

    /**
     * @Author: hzwlxy
     * @Email: 120235331@qq.com
     * @Date: 2019/7/2 16:43
     * @Description:获取所有路由路径和名称
     */
    public function routes()
    {
        $routes = Route::getRoutes();

        foreach ($routes as $route) {
            try {
                implode($route->gatherMiddleware(), ',');
            } catch (\Exception $e) {
                echo "请禁用laravel debuger插件";
                die;
            }
            echo "中间件:" . implode($route->gatherMiddleware(), ',') . '<br>';
            echo "地址:" . $route->uri . '<br />';
            echo "路由名称:" . $route->getName() . '<br />';
            echo "方法:" . $route->methods[0] . '<br />';
            echo '------------------------------------------------------------------------------------<br />';
        }
        return;
    }

    /**
     * @Author: hzwlxy
     * @Email: 120235331@qq.com
     * @Date: 2019/7/2 17:05
     * @Description:自动添加权限到permission
     */
    public function permission()
    {
        $routes = Route::getRoutes();
        $count = 0;
        foreach ($routes as $route) {

            try {
                implode($route->gatherMiddleware(), ',');
            } catch (\Exception $e) {
                echo "请禁用laravel debuger插件";
                die;
            }

            //需要校验的路由添加到permission里面
            if (in_array('enforcer:backend', $route->gatherMiddleware())) {

                $data["method"] = $route->methods[0];
                $data["route_name"] = $route->getName();
                $data["intro"] = $route->getName() . '[' . $route->methods[0] . ']';
                $data["guard_name"] = 'backend';

                $result = Permission::where("route_name", $data["route_name"])->where("method", $data["method"])->first();
                if (!$result) {
                    $count++;
                    Permission::create($data);
                    echo '添加:' . $route->getName() . '[' . $route->methods[0] . ']<br />';
                }
            }

            if (in_array('enforcer', $route->gatherMiddleware())) {
                $data["method"] = $route->methods[0];
                $data["route_name"] = $route->getName();
                $data["intro"] = $route->getName() . '[' . $route->methods[0] . ']';
                $data["guard_name"] = '';

                $result = Permission::where("route_name", $data["route_name"])->where("method", $data["method"])->first();
                if (!$result) {
                    $count++;
                    Permission::create($data);
                    echo '添加:' . $route->getName() . '[' . $route->methods[0] . ']<br />';
                }
            }
        }
        echo '.......ok,录入:' . $count . '条';
    }

    public function roles()
    {

        $role = Role::find(1);
        $route_name = request()->route()->getName();

        //添加permission
        if (false == Enforcer::hasPermissionForUser('role_id.' . $role->id, $route_name, 'GET')) {
            $addPermissionForUser = Enforcer::addPermissionForUser('role_id.' . $role->id, $route_name, 'GET');
        }
        dd($addPermissionForUser);

        //添加role
        $uid = Auth::guard("backend")->user()->getAuthIdentifier();
        $addRoleForUser = Enforcer::addRoleForUser('backend.user_id.' . $uid, 'role_id.' . $role->id);
        dd($addRoleForUser);

    }
}
